package com.gdcp.rv;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private Button edit_btn;
    private EditText edit_name;
    private EditText edit_age;
    private RadioButton edit_img1;
    private RadioButton edit_img2;
    private RadioButton edit_img3;
    private int imgId;
    private String name;
    private String no;
    private int age;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        getDate();
        initBundle();
       setDate();
        edit_btnEvent();

    }

    private void edit_btnEvent() {
        edit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 name=edit_name.getText().toString();
                 age= Integer.parseInt(edit_age.getText().toString());
                 imgId=R.drawable.dog1;

                if (edit_img1.isChecked()){
                    imgId=R.drawable.dog1;
                }
                else if (edit_img2.isChecked()){
                    imgId=R.drawable.dog2;
                }
                else if(edit_img3.isChecked()){
                    imgId=R.drawable.dog3;
                }


                        Intent intent=new Intent();
                intent.putExtra("NAME",name);
                intent.putExtra("AGE",age);
                intent.putExtra("ID",imgId);
                intent.putExtra("NO",no);
                setResult(1000, intent);
                     finish();

            }
        });

    }
/*             设置控件的数据    */
    private void setDate() {
           edit_name.setText(name);
           edit_age.setText(String.valueOf(age));

       switch (imgId){
           case R.drawable.dog1:
               edit_img1.setChecked(true);
               break;

           case R.drawable.dog2:
               edit_img2.setChecked(true);
               break;

           case R.drawable.dog3:
               edit_img3.setChecked(true);
               break;
       }
    }
    /*         绑定控件   */
    private void initBundle() {
         edit_age=findViewById(R.id.edit_age);
         edit_btn=findViewById(R.id.edit_btn);
         edit_name=findViewById(R.id.edit_name);
         edit_img1=findViewById(R.id.edit_img1);
         edit_img2=findViewById(R.id.edit_img2);
         edit_img3=findViewById(R.id.edit_img3);
    }

       /*    从上一个界面中的intent中获取数据   */
    private void getDate() {
        Intent intent=getIntent();
         name=intent.getStringExtra("NAME");
         age=intent.getIntExtra("AGE",0);
         imgId=intent.getIntExtra("ID",R.drawable.dog1);
         no=intent.getStringExtra("NO");

    }
}
